<template>
  <div class="app-container">
    <Search-content :queryParams="queryParams" labelWidth="120px" @query="handleQuery" @reset="resetQuery">
      <el-col :span="6">
        <el-form-item label="车牌号:">
          <el-input v-model.trim="queryParams.carNo" :maxlength="30" clearable placeholder="请输入车牌号"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车架号:">
          <el-input v-model.trim="queryParams.vinNo" :maxlength="30" clearable placeholder="请输入车架号"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="品牌/车系/车型:">
          <linkage-select @changeNode="linkageChange" v-model="linkageList" placeholder="请选择品牌/车系/车型"/>
        </el-form-item>
      </el-col>
      <!-- <el-col :span="6">
        <el-form-item label="牌照性质:">
          <el-input v-model="queryParams.licenseNature" clearable placeholder="请输入牌照性质"/>
        </el-form-item>
      </el-col> -->

      <el-col :span="6">
        <el-form-item label="排放标准:">
          <el-input v-model.trim="queryParams.environmentalStandards" :maxlength="30" clearable
                    placeholder="请输入排放标准"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="车辆产权:">
          <el-input v-model.trim="queryParams.propertyRightName" :maxlength="30" clearable
                    placeholder="请输入车辆产权"/>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="预警状态:">
          <el-select v-model="queryParams.warningState" placeholder="请选择预警状态" clearable>
            <el-option v-for="dict in dict.type.warning_state_type" :key="dict.value" :label="dict.label"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item label="预警生成时间:">
          <el-date-picker v-model="valueDate" type="daterange" placeholder="请选择日期" range-separator="-"
                          @change="handleDate" value-format="yyyy-MM-dd"
                          start-placeholder="开始日期" end-placeholder="结束日期" style="width: 100%;"/>
        </el-form-item>
      </el-col>

    </Search-content>

    <div class="card-wrap card-padding">
      <div class="table-title-wrap justify-end">
        <el-button size="mini" v-hasPermi="['routineForewarning:routineForewarningList:disposition']"
                   @click="handleDisposition"
                   type="primary">常规处置预警配置
        </el-button>
      </div>

      <table-page :loading="loading" :dataList="dataList" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @setSizeCurrent="handleList" border>

        <template slot="tableColAc">
          <el-table-column label="操作" align="center" fixed="left" width="120">
            <template #default="{row}">
              <el-button size="mini" type="text" @click="handleDisposeCar(row, 'view')"
                         v-if="row.warningState === '0'"
                         v-hasPermi="['routineForewarning:routineForewarningList:disposeCar']">
                处置车辆
              </el-button>

              <el-button size="mini" type="text" @click="handleDisposeDetail(row)"
                         v-if="row.warningState === '1'"
                         v-hasPermi="['routineForewarning:routineForewarningList:disposeDetail']"
              >
                查看处置详情
              </el-button>

            </template>
          </el-table-column>

        </template>
        <template slot="tableCol">

          <el-table-column align="center" label="车牌号" min-width="120" show-overflow-tooltip prop="carNo"/>

          <el-table-column align="center" label="车架号" min-width="180" show-overflow-tooltip prop="vinNo"/>

          <el-table-column align="center" label="品牌/车型/车款" show-overflow-tooltip min-width="400">
            <template #default="{row}">
              {{ row.brandName }}{{ row.seriesName }}{{ row.modelName }}
            </template>
          </el-table-column>

          <!-- <el-table-column align="center" label="牌照性质" show-overflow-tooltip min-width="130"
                           prop="licenseNature"/> -->

          <el-table-column align="center" label="排放标准" show-overflow-tooltip min-width="130"
                           prop="environmentalStandards"/>

          <el-table-column align="center" label="登记日期" show-overflow-tooltip min-width="100"
                           prop="registerTime">
            <template #default="{row}">
              {{ row.registerTime ? $dayjs(row.registerTime).format('YYYY-MM-DD') : '' }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="预警车龄" show-overflow-tooltip min-width="100"
                           prop="warningCarAge"/>

          <el-table-column align="center" label="预警闲置天数" show-overflow-tooltip min-width="100"
                           prop="warningDay"/>

          <el-table-column align="center" label="车辆产权" show-overflow-tooltip min-width="180"
                           prop="propertyRightName"/>

          <el-table-column align="center" label="上三年出险金额(元)" show-overflow-tooltip min-width="140"
                           prop="accidentAmountThreeYears"/>

          <el-table-column align="center" label="指导价(元)" show-overflow-tooltip min-width="120"
                           prop="guidancePrice">
            <template #default="{row}">
              {{ format_thousand(row.guidancePrice) }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="采购价(元)" show-overflow-tooltip min-width="120"
                           prop="purchasePrice">
            <template #default="{row}">
              {{ format_thousand(row.purchasePrice) }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="预警状态" show-overflow-tooltip min-width="120"
                           prop="warningState">
            <template #default="{row}">
              {{ selectDictLabel(dict.type.warning_state_type, row.warningState) }}
            </template>
          </el-table-column>

          <el-table-column align="center" label="预警生成时间" show-overflow-tooltip min-width="120"
                           prop="warningDate">
            <template #default="{row}">
              {{ row.warningDate ? $dayjs(row.warningDate).format('YYYY-MM-DD') : '' }}
            </template>
          </el-table-column>

        </template>


      </table-page>
    </div>

    <!--常规处置预警配置-->
    <el-dialog title="常规处置预警配置" :visible.sync="dialogBoole" width="600px">
      <el-form :model="form" label-width="110px" ref="formForm">
        <el-form-item label="车龄" prop="warningCarAge"
                      :rules="{required:true, message: '车龄不能为空', trigger: 'blur',type:'string'}">
          <div style="display: flex;align-items: center">
            <el-radio v-model="form.isCheckAge" label="2" border size="small">不校验此项</el-radio>
            <el-radio v-model="form.isCheckAge" label="1" border size="small">校验此项</el-radio>
            <el-input v-model="form.warningCarAge" @change="handleChange('warningCarAge')">
              <template slot="append">年及以上</template>
            </el-input>
            <el-tooltip style="padding-left: 10px" class="item" effect="dark" content="输入1~99" placement="top-start">
              <i class="el-icon-question"/>
            </el-tooltip>
          </div>
        </el-form-item>
        <el-form-item label="在库闲置天数" prop="warningDay"
                      :rules="{required:true, message: '在库闲置天数不能为空', trigger: 'blur',type:'string'}">
          <div style="display: flex;align-items: center">
            <el-radio v-model="form.isCheckDay" label="2" border size="small">不校验此项</el-radio>
            <el-radio v-model="form.isCheckDay" label="1" border size="small">校验此项</el-radio>
            <el-input v-model="form.warningDay" @change="handleChange('warningDay')">
              <template slot="append">
                天及以上
              </template>
            </el-input>
            <el-tooltip style="padding-left: 10px" class="item" effect="dark" content="输入1~9999"
                        placement="top-start">
              <i class="el-icon-question"/>
            </el-tooltip>
          </div>
        </el-form-item>
        <span style="color: red" v-if="handleBoole(form)">请至少配置一条条预警条件</span>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleDialog(1)">取 消</el-button>
        <el-button type="primary" v-if="!(handleBoole(form))" @click="handleDialog(2)">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getVehicleDisposalWarning, warningConfig, getWarningConfig} from "@/api/vehicleDisposal/routineForewarning.js"
import SearchContent from "@/components/SearchContent/index.vue";
import LinkageSelect from "../../../components/linkageSelect";

export default {
  name: 'RoutineForewarningList',//常规处置预警列表
  dicts: ['amortization_status', 'warning_state_type'],
  components: {
    SearchContent, LinkageSelect
  },
  computed: {
    handleBoole() {
      return function (val) {
        if (val.isCheckAge === '2' && val.isCheckDay === '2') {
          return true
        } else {
          return false
        }
      }
    }
  },
  watch: {},
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        carNo: '',
        vinNo: '',
        brandId: '',
        seriesId: '',
        modelId: '',
        licenseNature: '',
        environmentalStandards: '',
        propertyRightName: '',
        warningState: '',
        warningStartDate: '',
        warningEndDate: '',
      },
      valueDate: [],
      linkageList: [],
      dataList: [
        {
          carNo: '111111'
        }
      ],
      total: 0,
      loading: false,
      dialogBoole: false,
      form: {
        isCheckAge: '1',
        isCheckDay: '2',
        warningDay: '',
        warningCarAge: ''
      },
    };
  },
  created() {
    this.handleList()
  },
  mounted() {
  },
  methods: {
    handleDate(val) {
      if (val) {
        this.valueDate = val
        this.queryParams.warningStartDate = val[0]
        this.queryParams.warningEndDate = val[1]
      } else {
        this.valueDate = []
        this.queryParams.warningStartDate = ''
        this.queryParams.warningEndDate = ''
      }
    },

    handleQuery() {
      this.queryParams.pageNum = 1;
      this.handleList()
    },

    resetQuery() {
      this.linkageList = []
      this.valueDate = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        carNo: '',
        vinNo: '',
        brandId: '',
        seriesId: '',
        modelId: '',
        licenseNature: '',
        environmentalStandards: '',
        propertyRightName: '',
        warningState: '',
        warningStartDate: '',
        warningEndDate: '',
      }
      this.handleList()
    },

    linkageChange(val) {
      this.linkageList = val;
      if (val) {
        this.queryParams.brandId = val[0] ? val[0].id : "";
        this.queryParams.seriesId = val[1] ? val[1].id : "";
        this.queryParams.modelId = val[2] ? val[2].id : "";
      } else {
        this.queryParams.brandId = "";
        this.queryParams.seriesId = "";
        this.queryParams.modelId = "";
      }
      this.$forceUpdate();
    },

    //列表
    async handleList() {
      this.loading = true
      try {
        const res = await getVehicleDisposalWarning(this.queryParams)
        this.dataList = res.rows
        this.total = res.total
      } catch (e) {
        console.error('获取失败', e)
      } finally {
        this.loading = false
      }
    },

    //常规处置预警配置
    handleDisposition() {
      this.dialogBoole = true
      getWarningConfig().then(res => {
        if (res.code === 200) {
          this.form = res.data
        }
      })
    },

    //效验 年龄 在库zand 天数
    handleChange(str) {
      let reg = /^[1-9]\d*$/
      if (this.form[str] && !reg.test(this.form[str])) {
        this.form[str] = ''
      } else {
        if (str === 'warningCarAge') {
          let reg1 = /^(?:[1-9]|[1-9]\d)$/
          if (!reg1.test(this.form[str])) {
            this.form[str] = ''
          }
        } else {
          let reg2 = /^(?:[1-9]\d{0,3}|9999)$/
          if (!reg2.test(this.form[str])) {
            this.form[str] = ''
          }
        }

      }
    },

    //弹框确定
    handleDialog(val) {
      if (val === 1) {
        if (this.$refs.formForm) {
          this.$refs.formForm.resetFields();
        }
        this.dialogBoole = false
      }

      if (val === 2) {
        this.$refs.formForm.validate((valid) => {
          if (valid) {
            warningConfig(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success('配置成功')
                this.queryParams.pageNum = 1;
                this.handleList()
                this.dialogBoole = false
              }
            })
          }
        })
      }
    },

    //处置车辆
    handleDisposeCar(row) {
      this._toPage({
        path: "/VehicleDisposal/RoutineDisposal/RoutineApplyForNew",
        name: 'RoutineApplyForNew',
        query: {
          type: "edit",
          path: 'RoutineForewarningList',
          carId: row.carId
        }
      })
    },

    //查看处置详情
    handleDisposeDetail(row) {
      this._toPage({
        path: "/VehicleDisposal/RoutineDisposal/RoutineHandleDetails",
        name: "RoutineHandleDetails",
        query: {
          type: "view",
          id: row.vehicleDisposalId,
          flowId: row.flowId,
          path: 'RoutineForewarningList',
        },
      })
    }
  },
};
</script>

<style scoped lang="scss">
::v-deep .el-input-group__append {
  padding-left: 5px !important;
  padding-right: 5px !important;
  background-color: #fff;
}
</style>


